package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.dto.Dishdto;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    public DishFlavorService dishFlavorService;

    //添加
    @PostMapping
    public R<String> save(@RequestBody Dishdto dishdto){

        dishService.saveWithFlavor(dishdto);

        return R.success("添加成功");
    }

    //分页
    @GetMapping("/page")
    public R<Page> getpage(Integer page,Integer pageSize,String name) {

        //实现两个对象数据的复制
        //（源对象，目标对象，忽略拷贝的字段）
        //BeanUtils.copyProperties(,,,);

        Page<Dish> page1 = new Page<>(page, pageSize);


        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(name), Dish::getName, name).orderByDesc(Dish::getUpdateTime);

        Page<Dish> page2 = dishService.page(page1, wrapper);

        Page<Dishdto> pagedto = new Page<>(page, pageSize);

        List<Dish> dishList = page1.getRecords();
        List<Dishdto> dishdtoList = new ArrayList<>();
        for (Dish dish: dishList){
            Dishdto dishdto = new Dishdto();
            BeanUtils.copyProperties(dish,dishdto);

            Long categoryId = dish.getCategoryId();
            Category byId = categoryService.getById(categoryId);

            dishdto.setCategoryName(byId.getName());
            dishdtoList.add(dishdto);
        }

        pagedto.setRecords(dishdtoList);
        pagedto.setTotal(page1.getTotal());
        return R.success(pagedto);
    }

    //修改界面回显
    @GetMapping("/{id}")
    public R<Dishdto> getdto(@PathVariable Long id){

        Dishdto dishdto = dishService.getByIdWithFlavor(id);

        return R.success(dishdto);

    }

    //修改
    @PutMapping
    public R<String> update(@RequestBody Dishdto dishdto){

        dishService.updateWithFlavor(dishdto);
        return R.success("修改成功");
    }

    //删除
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){

        dishService.removeWith(ids);

        return R.success("删除成功");

    }

    //修改状态
    @PostMapping("/status/{st}")
    public R<String> destatus(@PathVariable int st,@RequestParam List<Long> ids){
        List<Dish> dishes = ids.stream().map((item) -> {
            Dish dish = new Dish();
            dish.setId(item);
            dish.setStatus(st);
            return dish;
        }).collect(Collectors.toList());

        dishService.updateBatchById(dishes);


        return R.success("修改成功");

    }


    //setmeal相关
    @GetMapping("/list")
    public R<List> getlist(Long categoryId){

        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(Dish::getCategoryId,categoryId);



        List<Dish> list = dishService.list(wrapper);

        return R.success(list);



    }


}
